package com.lb.sssq.util.webpower;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.edais.service.UserService;
import com.lb.sssq.domain.TMsgThirdPlan;
import com.lb.sssq.domain.Template;
import com.lb.sssq.domain.ThirdMessage;
import com.lb.sssq.domain.UserSession;
import com.lb.sssq.service.TMsgThirdPlanService;
import com.lb.sssq.service.TUserService;
import com.lb.sssq.service.TemplateService;
import com.lb.sssq.service.ThirdMessageService;
import com.lb.sssq.util.DateUtility;


@Component
@Repository
public class PowerSendAndInsert {
	
	private Log logger=LogFactory.getLog(PowerSendAndInsert.class);

	/**
	 * Mobile
	 * author  guanxuxing
	 * webPower第三方短信推送
	 * 添加第三方报文信息
	 */
	public void sendAndInsert(HttpServletRequest request,TemplateService templateService,
			ThirdMessageService thirdMessageService,TUserService tUserService, 
			String type,String Template_key,String[] message,Integer userSessionId
			,TMsgThirdPlanService tMsgThirdPlanService,UserService userService){
		        //author   guanXuXing   Mobile
				//添加webPower第三方报文信息  begin	
				try {
					logger.info("webPower  begin--");
					UserSession userSession=tUserService.getMobileById(userSessionId);
					String mobile=userSession.getMobile();
					//推送消息获取
					Template template=new Template();
					template.setType(type);
					template.setTemplate_key(Template_key);
					Template tempMessage=templateService.getTemplateByKeyAndType(template);
					String content=tempMessage.getContent();
					String flag=tempMessage.getWhen_then_push();
					//是否进行短信推送
					if(flag.toUpperCase().equals("Y")){
					//处理短信推送信息
		            content=MessageFormat.format(content,message);
					//调用第三方工程进行短信推送			
					Map<String, String> paraMap=new HashMap<String, String>();
					//判断短信推送的渠道
					ArrayList<TMsgThirdPlan> tMsgThirdPlans=tMsgThirdPlanService.getTMsgThirdPlan();
					TMsgThirdPlan ob = tMsgThirdPlans.get(0);
					String platEnName = String.valueOf(ob.getPlatenname());
					int plateId = Integer.valueOf(String.valueOf(ob.getId()));				
					//处理短信推送信息
					//调用第三方工程进行短信推送			
					String receiveMessage="";
					if("WEBPOWER".equals(platEnName)){
						paraMap.put("mobile", mobile);
						paraMap.put("content", content);
						paraMap.put("campaignID", "1");
						receiveMessage=TdRequestUtil.sendTd(paraMap);
					}else if("DAHST".equals(platEnName)){
						paraMap.put("content", content);
						paraMap.put("phoneNum", mobile);
						receiveMessage=userService.sendSmsW(request, paraMap);	
					}
					JSONObject jsonObject=JSONObject.fromObject(receiveMessage);
					//添加报文信息
					ThirdMessage thirdMessage=new ThirdMessage();
					thirdMessage.setSendmessage(content);
					thirdMessage.setReceivemessage(receiveMessage);
					// 判断发送渠道  WECHAT  ANDROID  IOS
					String sendchannel=request.getHeader("User-Agent");
					logger.info("发送渠道-begin-sendchannel--->"+sendchannel);	
					if(sendchannel!=null&&!sendchannel.equals("")){
					if(sendchannel.toLowerCase().contains("android")){
						sendchannel="ANDROID";
					}
					else{
						sendchannel="IOS";
					}
					}
					else{
						sendchannel="APP";
					}
					thirdMessage.setSendchannel(sendchannel);
					logger.info("发送渠道-end-sendchannel---->"+sendchannel);
					thirdMessage.setSendtype(tempMessage.getSubject());
					thirdMessage.setPlatid(plateId);		
					 thirdMessage.setSendtime(DateUtility.getDateDetail(DateUtility.getStrDateDetail(new Date())));
					//解析返回过来的结果  begin
					if(jsonObject.containsKey("status")){                  //webpower
						 String status=jsonObject.get("status").toString();
						 String scheduleTime=jsonObject.get("scheduleTime").toString();						
						 thirdMessage.setBacktime(DateUtility.getDateDetail(scheduleTime));
						 thirdMessage.setUpdatedate(DateUtility.getDateDetail(scheduleTime));
						if(status.toUpperCase().equals("OK")){
							thirdMessage.setSendstatus("SUCCESS");
						}else {
							thirdMessage.setSendstatus("FAILD");
						}
					}else if(jsonObject.containsKey("rescode")){            //大汉三通
						thirdMessage.setBacktime(DateUtility.getDateDetail(DateUtility.getStrDateDetail(new Date())));
						thirdMessage.setUpdatedate(DateUtility.getDateDetail(DateUtility.getStrDateDetail(new Date())));
						String rescode = jsonObject.getString("rescode");
						if("00000".equals(rescode)){
							thirdMessage.setSendstatus("SUCCESS");
						}else{
							thirdMessage.setSendstatus("FAILD");
						}
					}else{
						thirdMessage.setSendstatus("FAILD");
					}
					//end
					thirdMessage.setMediatype("MOBILE");
					thirdMessage.setMeaidaddr(mobile);
					thirdMessage.setCreateper(userSession.getId().toString());
					thirdMessage.setCreatedate(DateUtility.getDateDetail(DateUtility.getStrDateDetail(new Date())));
					thirdMessage.setUpdateper(userSession.getId().toString());					
					thirdMessageService.insertThidMessage(thirdMessage);					
					logger.info("messageJson-------"+receiveMessage);
					logger.info("webPower  end--");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					logger.info("webPower-Exception-mobile-----------"+e.getMessage());
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
				//添加webPower第三方报文信息  end
	}

	
}
